<template>
  <div v-show="dialogValue" class="add-form f-c">
    <div class="header">
      <div class="title">
        添加/编辑人员
      </div>
      <el-button @click="onPageBack">返回</el-button>
    </div>
    <div class="add-form-content f-g">
      <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @close="onPageBack"
        @save="onSave"
      />
    </div>
  </div>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  props: {
    deptId: String
  },
  data() {
    return {
      dialogValue: false,
      ruleForm: {
        id: undefined,
        sysUserId: ''
      },
      rules: {
        sysUserId: [{ required: true, message: '不能为空' }]
      },
      formList: [
        {
          key: 'sysUserId',
          name: 'select',
          label: '用户',
          colSpan: 12,
          options: []
        }
      ]
    }
  },
  methods: {
    async open(item) {
      if (item) {
        for (const key in this.ruleForm) {
          this.ruleForm[key] = item[key]
        }
      }
      http.user.page().then(res => {
        this.formList[0].options = res.data.records.map(el => {
          return {
            value: el.id,
            name: el.realname
          }
        })
      })
      this.dialogValue = true
    },
    onPageBack() {
      this.$emit('get-page')
      this.dialogValue = false
    },
    onSave() {
      http.dept_user[this.ruleForm.id ? 'update' : 'create']({ ...this.ruleForm, departId: this.deptId }).then(res => {
        this.$message.success(res.msg)
        this.onPageBack()
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
